<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账号" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="密码" prop="password">-->
<!--      <el-input-->
<!--        v-model="queryParams.password"-->
<!--        placeholder="请输入密码"-->
<!--        clearable-->
<!--        @keyup.enter.native="handleQuery"-->
<!--      />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="盐" prop="salt">-->
<!--        <el-input-->
<!--          v-model="queryParams.salt"-->
<!--          placeholder="请输入盐"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="认证状态" prop="authentication">-->
<!--        <el-input-->
<!--          v-model="queryParams.authentication"-->
<!--          placeholder="请输入认证状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否vip" prop="vip">-->
<!--        <el-input-->
<!--          v-model="queryParams.vip"-->
<!--          placeholder="请输入是否vip"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户微信openid" prop="openId">-->
<!--        <el-input-->
<!--          v-model="queryParams.openId"-->
<!--          placeholder="请输入用户微信openid"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="可用余额" prop="balance">-->
<!--        <el-input-->
<!--          v-model="queryParams.balance"-->
<!--          placeholder="请输入可用余额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="积分" prop="point">-->
<!--        <el-input-->
<!--          v-model="queryParams.point"-->
<!--          placeholder="请输入积分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="邀请编码" prop="inviteCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.inviteCode"-->
<!--          placeholder="请输入邀请编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="会话密钥" prop="sessionKey">-->
<!--        <el-input-->
<!--          v-model="queryParams.sessionKey"-->
<!--          placeholder="请输入会话密钥"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['charge:charge_user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['charge:charge_user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['charge:charge_user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['charge:charge_user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="charge_userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="账号" align="center" prop="uname" />
      <el-table-column label="昵称" align="center" prop="username" />
      <el-table-column label="密码" align="center" prop="password"  />
      <el-table-column label="盐" align="center" prop="salt" />
      <el-table-column label="头像地址" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" width="100"/>
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="认证状态" align="center" prop="authentication" #default="scp">
        <span v-if="scp.row.authentication==0">成功</span>
        <span v-if="scp.row.authentication==1">失败</span>
      </el-table-column>
      <el-table-column label="是否vip" align="center" prop="vip" #default="scp">
        <span v-if="scp.row.vip==0">普通用户</span>
        <span v-if="scp.row.vip==1">vip用户</span>
      </el-table-column>
      <el-table-column label="用户微信openid" align="center" prop="openId" />
      <el-table-column label="可用余额" align="center" prop="balance" />
      <el-table-column label="积分" align="center" prop="point" />
      <el-table-column label="邀请编码" align="center" prop="inviteCode" />
      <el-table-column label="用户类型" align="center" prop="type" #default="scp">
        <el-tag :type="scp.row.type === 0 ? 'primary' : 'success'">
          {{ scp.row.type === 0 ? '充电用户' : '投资人' }}
        </el-tag>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="status" #default="scp">
        <el-tag :type="scp.row.status === 0 ? 'info' : 'warning'">
          {{ scp.row.status === 0 ? '普通用户' : '投资老板' }}
        </el-tag>
      </el-table-column>
      <el-table-column label="会话密钥" align="center" prop="sessionKey" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['charge:charge_user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['charge:charge_user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账号:" prop="uname">
          <el-input v-model="form.uname" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="昵称:" prop="username">
          <el-input v-model="form.username" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="头像地址:" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="手机号:" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="认证状态:" prop="authentication">
          <el-input v-model="form.authentication" placeholder="请输入认证状态 0成功 1失败" />
        </el-form-item>
        <el-form-item label="是否vip:" prop="vip">
          <el-input v-model="form.vip" placeholder="请输入是否vip 0普通用户 1vip用户" />
        </el-form-item>
        <el-form-item label="用户微信openid:" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入用户微信openid" />
        </el-form-item>
        <el-form-item label="可用余额:" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入可用余额" />
        </el-form-item>
        <el-form-item label="积分:" prop="point">
          <el-input v-model="form.point" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="邀请编码:" prop="inviteCode">
          <el-input v-model="form.inviteCode" placeholder="请输入邀请编码" />
        </el-form-item>
        <el-form-item label="用户类型:" prop="type">
          <el-input v-model="form.inviteCode" placeholder="请输入用户类型 0充电用户 1投资人" />
        </el-form-item>
        <el-form-item label="用户状态:" prop="status">
          <el-input v-model="form.inviteCode" placeholder="请输入用户状态 0普通用户 1投资老板" />
        </el-form-item>
        <el-form-item label="会话密钥:" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="请输入会话密钥" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCharge_user, getCharge_user, delCharge_user, addCharge_user, updateCharge_user } from "@/api/charge/charge_user";

export default {
  name: "Charge_user",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      charge_userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uname: null,
        username: null,
        password: null,
        salt: null,
        avatar: null,
        phone: null,
        email: null,
        authentication: null,
        vip: null,
        openId: null,
        balance: null,
        point: null,
        inviteCode: null,
        type: null,
        status: null,
        sessionKey: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listCharge_user(this.queryParams).then(response => {
        this.charge_userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uname: null,
        username: null,
        password: null,
        salt: null,
        avatar: null,
        phone: null,
        email: null,
        authentication: null,
        vip: null,
        openId: null,
        balance: null,
        point: null,
        inviteCode: null,
        type: null,
        status: null,
        createTime: null,
        updateTime: null,
        sessionKey: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCharge_user(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCharge_user(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCharge_user(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + ids + '"的数据项？').then(function() {
        return delCharge_user(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('charge/charge_user/export', {
        ...this.queryParams
      }, `charge_user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
